package com.dranie.servlet;

import org.apache.commons.lang.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

/**
 * Created with IntelliJ IDEA.
 * User: xi
 * Date: 13-8-14
 * Time: 上午11:21
 * To change this template use File | Settings | File Templates.
 */
public class ProgressServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String percentName = request.getParameter("percentName");
        if (StringUtils.isNotBlank(percentName)) {
            PrintWriter out = response.getWriter();
            Object value = request.getSession().getAttribute(percentName);
            out.print(value);
            out.flush();
            out.close();
            Integer v = Integer.valueOf(value.toString());
            if (v >= 100 || v < 0) {
                request.getSession().removeAttribute(percentName);
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String percentName = request.getParameter("percentName");
        String reset = request.getParameter("reset");
        if (StringUtils.isNotBlank(reset) && reset.equals("true")) {
            request.getSession().removeAttribute(percentName);
        }
    }
}
